//
//  CZWelcomeViewController.swift
//  weibo10
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 apple. All rights reserved.
/*
1 懒加载  背景图片的view ／头像view ／ messageLabel
2 添加子控件
3 给懒加载属性添加约束
4 设置图片圆角
5 头像动画
  在viewDidAppear里调用动画
  创建动画
  用snp_updateConstraints更新约束
  根据约束重新设置frame值  self.view.layoutIfNeeded()
*/

import UIKit
import SnapKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        prepareUI()
        
        setIcon()
        
        //获取用户信息
        CZUserAccountViewModel.sharedInstance.loadUserInfo { () -> () in
            self.setIcon()
        }

    }
    
    
    private func setIcon()
    {
    if let avater_large = CZUserAccountViewModel.sharedInstance.userAccount?.avatar_large
    {
        //获取到了用户数据，设置头像
        let url = NSURL(string: avater_large)
        self.iconView.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar_default_big"))
        }
    
    }
    
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //在用户看到界面时调用动画
        moveAnimation()
    }
    
    //创建移动动画（view的动画）
    private func moveAnimation()
    {
    
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                //更新底部约束
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            //根据约束重新设置frame值
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
//                CZPrint(items: "动画完成")
                
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        //切换到mainViewController
                        let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                        appDelegate.switchRootViewController(CZMainTabBarController())
                })
                
        }
    
    }
    
    
    //添加子控件
    private func prepareUI()
    {
       view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
     //添加约束
        //bkgView 
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        //iconView
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        //label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    
    }
    
    
    
  //MAEK: - 懒加载
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconView: UIImageView =
    
    {
    let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //图片圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    //messageLabel
    private lazy var messageLabel: UILabel =
    {
        //创建label
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        
       label.alpha = 0
        return label
    }()
}

    


